package com.bona.service.report;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bona.dao.common.BaseDao;
import com.bona.dao.report.FormFieldDao;
import com.bona.dao.report.FormHeadDao;
import com.bona.model.report.FormFieldModel;
import com.bona.model.report.FormHeadModel;
import com.bona.service.common.BaseService;
import com.bona.util.common.StringUtil;
import com.bona.util.common.UUIDGenerator;

@Service("formHeadService")
@Transactional
public class FormHeadService extends BaseService {

	@Autowired
	private FormHeadDao rormHeadDao;
	@Autowired
	private FormFieldDao formFieldDao;
	
	@Override
	public BaseDao getDao() {
		return rormHeadDao;
	}

	/**
	 * 添加或更新分组信息
	 * @param model
	 */
	public void addOrUpdate(FormHeadModel model) {
		
		if (StringUtil.isEmpty(model.getId())) {	// 新增分组信息
			model.setId(UUIDGenerator.generate());
			rormHeadDao.add(model);
			for (FormFieldModel formField: model.getColumns()) {
				formField.setId(UUIDGenerator.generate());
				formField.setTableId(model.getId());
				formFieldDao.add(formField);
			}
			return;
		}
		rormHeadDao.update(model);
		FormFieldModel formFieldModel = new FormFieldModel();
		formFieldModel.setTableId(model.getId());
		formFieldDao.delete(formFieldModel);
		for (FormFieldModel formField: model.getColumns()) {
			formField.setId(UUIDGenerator.generate());
			formField.setTableId(model.getId());
			formFieldDao.add(formField);
		}
	}

}
